package com.world.cat.controller.sys;

import com.world.cat.model.sys.Config;
import com.world.cat.service.generate.FieldService;
import com.world.cat.service.sys.ConfigService;
import com.world.common.pojo.Result;
import com.world.common.pojo.WorldPage;
import com.world.common.util.NumberUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/sys/config")
public class ConfigController {
    @Resource
    private ConfigService service;

    @RequestMapping(value = "/common")
    public String common(Model model) {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Config.class));
        model.addAttribute("qClassActions", FieldService.getClassActionInfo(Config.class));
        model.addAttribute("config", ConfigService.configMap);
        return "/sys/config/common";
    }

    @RequestMapping(value = "/prize")
    public String prize(Model model) {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Config.class));
        model.addAttribute("qClassActions", FieldService.getClassActionInfo(Config.class));
        model.addAttribute("config", ConfigService.configMap);
        return "/sys/config/prize";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String params, HttpServletRequest request, WorldPage worldPage) throws Exception {
        worldPage.setPageIndex(NumberUtil.intValue(request.getParameter("pagenum")) + 1);
        worldPage.setPageSize(NumberUtil.intValue(request.getParameter("pagesize")));
        worldPage.setProperties("createTime");
        Page<Config> pages = service.findPage(Config.class, params, worldPage.getPageRequest());
        return pages;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(String params) throws Exception {
        Result result = service.save(Config.class, params);
        return result;
    }

    @RequestMapping(value = "/saveParam")
    @ResponseBody
    public Object saveParam(String configKey, String configValue) throws Exception {
        Result result = service.saveParam(configKey, configValue);
        return result;
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Object del(String[] ids) {
        service.del(ids);
        return new Result(true);
    }

    @RequestMapping(value = "/edit")
    public String edit(Model model, String id) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Config.class));
        Config object = service.get(id);
        model.addAttribute("obj", object);
        return "/back/sysConfig/edit";
    }

    @RequestMapping(value = "/add")
    public String add(Model model) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Config.class));
        return "/back/sysConfig/add";
    }

    @RequestMapping(value = "/view")
    public String view(Model model, String id) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Config.class));
        Config object = service.get(id);
        model.addAttribute("obj", object);
        return "/back/sysConfig/view";
    }
}
